package com.kyub.test.model;

import com.kyub.spring.gears.services.ControllerDelegateService;

import java.util.*;
import java.io.Serializable;

import org.springframework.validation.Errors;
import org.springframework.web.context.request.WebRequest;

/**
 * Created by IntelliJ IDEA.
 * User: sreghenzi
 * Date: 2-gen-2009
 * Time: 16.32.13
 * To change this template use File | Settings | File Templates.
 */
public class MockPetDao implements ControllerDelegateService<Pet,WebRequest> {
    private List<Pet> a;

    public MockPetDao() {
        a = new ArrayList();
        for (int i = 0; i < 10; i++) {
            a.add(new Pet(Long.decode("" + i), "Camaleont " + i, i % 2 > 0, new Date()));

        }
    }

    public List list() {

        return a;
    }

    public Pet load(Serializable id) {
        return  a.get(Integer.valueOf(id.toString()));
    }

    public void save(Pet command) {

        Pet p = (Pet) command;
       
        if (p.getId() == null){
            p.setId(Long.valueOf(a.size()));
             a.add(p);
        }

    }

    public void delete(Pet load) {
       // System.out.println("a.size() = " + a.size());
        a.remove(load);
      //  System.out.println("a.size() = " + a.size());

    }

    public void validate(Object o, Errors errors) {
        Pet p = (Pet) o;

        System.out.println("Valido: " + p);

        System.out.println("Owner: " + p.getOwner());

        if (p.getName() == null || p.getName().length() == 0)
            errors.rejectValue("name", "NomeObbligatorio");
        else if (p.getName().indexOf("@") > -1 || p.getName().indexOf(";") > -1)
            errors.rejectValue("name", "NomeInvalido");

        System.out.println("Errori? " + errors.hasErrors());

    }



    public List list(WebRequest request) {
        final String s = request.getParameter("_searchString");
        final String page = request.getParameter(ControllerDelegateService.LIST_PAGE_PARAM);
        Integer k = 0;
        if(page != null){
             k =Integer.parseInt(page)-1;
        }
        k= k*20;
        System.out.println("search for " + s);

        int count = 0;
         int count2 = 0;

        if (s != null) {
            ArrayList l = new ArrayList();
            Iterator i = a.iterator();
            
            while (i.hasNext()) {
                count = count +1;
                Pet p = (Pet) i.next();
                if (p.getName().indexOf(s) > -1 && count > k && count2 < 20) {
                    count2 = count2 +1  ;
                    l.add(p);
                }
            }

            return l;
        }else{
            ArrayList l = new ArrayList();
            Iterator i = a.iterator();
              while (i.hasNext()) {
                  count = count +1;
                   Pet p = (Pet) i.next();
                  if ( count > k && count2 < 20) {
                    count2 = count2 +1  ;
                    l.add(p);

                  }
              }
            return l;
        }
    }

    @Override
    public Long count(WebRequest params) {
        return new Long(list().size());
    }

    
    public Map getFilters() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

	@Override
	public void delete(Pet load, WebRequest params) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Pet load(Serializable id, WebRequest params) {
		
		return load(id);
	}

	@Override
	public void save(Pet command, WebRequest params) {
		save(command);
		
	}
}
